package cn.edu.wynu.apis.collections.sets;

import cn.edu.wynu.apis.common.CommonFunctions;

import java.util.TreeSet;

public class Student implements Comparable{
    private String name;
    private int age;
    private int score;
    private String learnId;
     // 构造方法
    public Student(String name, int age, int score, String learnId) {
        this.name = name;
        this.age = age;
        this.score = score;
        this.learnId = learnId;
    }
     // 重写toString方法,方便打印
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                ", learnId='" + learnId + '\'' +
                '}';
    }

    public static void main(String[] args) {
        // 1. 创建学生对象
        Student stu1 = new Student("张三", 18, 90, "1001");
        Student stu2 = new Student("李四", 19, 85, "1002");
        Student stu3 = new Student("王五", 20, 95, "1003");
        System.out.println(stu1);
        System.out.println(stu2);
        System.out.println(stu3);

        TreeSet<Student> class2 = new TreeSet<>();
        class2.add(stu1);
        class2.add(stu2);
        class2.add(stu3);
        CommonFunctions.cutBlock("班级2学生,排序后");
        for (Student stu : class2) {
            System.out.println(stu);
        }
    }

    // 如果一个类想要使用TreeSet来进行排序,则需要:
    //   1. implements Comparable接口
    //   2. override compareTo方法
    //   3. compareTo方法中,可以按照自己想的方式排序:
    //      ①当前对象某个特征值 - 传入的对象的某个特征值, 则按照这个特征值升序
    //      ②传入的对象的某个特征值 - 当前对象某个特征值, 则按照这个特征值降序
    @Override
    public int compareTo(Object anotherStudent) {
         return this.score - ((Student) anotherStudent).score;
//        return this.age - ((Student) anotherStudent).age;
    }
}
